package com.agd.quick.states;
//a
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;
import lib.elementgame.GameSprite;
import lib.elementgame.GameText;
import lib.engine.GameEngine;
import lib.engine.GameState;

public class StateAchievements extends GameState {
        
        private static GameSprite backgroundFloor;
        private static GameSprite acvpic1;
        private static GameSprite acvpic2;
        private static GameSprite acvpic3;
        private static GameSprite acvpic4;
        private static GameSprite acvpic5;
        private static GameSprite acvpic6;
        private static GameSprite acvpic7;
        private static GameSprite acvpic8;
        private static GameSprite acvpic9;
        private static GameSprite acvpic10;
        private static GameSprite []acvpiclock = new GameSprite[10];
        
        private float y1;
        private float y2;
        
        
        
        private int Ach1;
        private int Ach2;
        private int Ach3;
        private int Ach4;
        private int Ach5;
        private int Ach6;
        private int Ach7;
        private int Ach8;
        private int Ach9;
        private int Ach10;
        
        private GameText achievement1;
        private GameText achievement2;
        private GameText achievement3;
        private GameText achievement4;
        private GameText achievement5;
        private GameText achievement6;
        private GameText achievement7;
        private GameText achievement8;
        private GameText achievement9;
        private GameText achievement10;
        
        private GameText achievementtxt1;
        private GameText achievementtxt2;
        private GameText achievementtxt3;
        private GameText achievementtxt4;
        private GameText achievementtxt5;
        private GameText achievementtxt6;
        private GameText achievementtxt7;
        private GameText achievementtxt8;
        private GameText achievementtxt9;
        private GameText achievementtxt10;

        private boolean keluar;
        public StateAchievements(GameEngine engine) {
                super(engine);
                // TODO Auto-generated constructor stub
        }

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                        ITouchArea pTouchArea, float pTouchAreaLocalX,
                        float pTouchAreaLocalY) {
                // TODO Auto-generated method stub
        		if(pSceneTouchEvent.isActionDown()){
        			y1 = pTouchAreaLocalY;
        		}
                if (pSceneTouchEvent.isActionMove()) {
                        y2 = pTouchAreaLocalY;
                        if(engine.camera.getCenterY() - (y2-y1) < 240){
                        	engine.camera.setCenter(engine.camera.getCenterX(), 240);
                        }
                        else if(engine.camera.getCenterY() - (y2-y1) > 720){
                        	engine.camera.setCenter(engine.camera.getCenterX(), 720);
                        }
                        else{
                        	engine.camera.setCenter(engine.camera.getCenterX(), engine.camera.getCenterY() - (y2-y1));
                        }
                }
                return false;
        }

        @Override
        public void initComponent() {
                // TODO Auto-generated method stub
                backgroundFloor = new GameSprite(LDR_SPRITE_BACKGROUND, engine);
                acvpic1 = new GameSprite(ACV_SPRITE_ACV1, engine);
                acvpic2 = new GameSprite(ACV_SPRITE_ACV2, engine);
                acvpic3 = new GameSprite(ACV_SPRITE_ACV3, engine);
                acvpic4 = new GameSprite(ACV_SPRITE_ACV4, engine);
                acvpic5 = new GameSprite(ACV_SPRITE_ACV5, engine);
                acvpic6 = new GameSprite(ACV_SPRITE_ACV6, engine);
                acvpic7 = new GameSprite(ACV_SPRITE_ACV7, engine);
                acvpic8 = new GameSprite(ACV_SPRITE_ACV8, engine);
                acvpic9 = new GameSprite(ACV_SPRITE_ACV9, engine);
                acvpic10 = new GameSprite(ACV_SPRITE_ACV10, engine);
                for(int i=0; i<10; i++){
                        acvpiclock[i] = new GameSprite(ACV_SPRITE_ACVLOCK, engine);
                }
                
                
                
                achievement1 = new GameText("Accidentaly Plow", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement2 = new GameText("Plow Lovers", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement3 = new GameText("Master Plower", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement4 = new GameText("Goddess of Plower", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement5 = new GameText("Live for plowing", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement6 = new GameText("Egg Collector ", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement7 = new GameText("My Own Gas Station ", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement8 = new GameText("You Deserve Your Glory ", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement9 = new GameText("Invisible Plower ", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                achievement10 = new GameText("Ruler of Plower ", 100, engine.getFont(FONT_CIRCULAT_16_BLACK), engine);
                
                
                achievementtxt1 = new GameText("Plow for 100 meters.",200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt2 = new GameText("Plow for 500 meters.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt3 = new GameText("Plow for 1000 meters.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt4 = new GameText("Plow for 1500 meters.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt5 = new GameText("Plow for 2500 meters.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt6 = new GameText("Collect 25 Eggs in One Plowing.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt7 = new GameText("Collect 25 Gas in One Plowing.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt8 = new GameText("Collect 25 Poop in One Plowingg.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt9 = new GameText("Plow for 500 meters without \ntaking any damage.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
                achievementtxt10 = new GameText("Plow for 500 meters without \nusing turbo.", 200, engine.getFont(FONT_CIRCULAT_14_BLACK), engine);
        }

        @Override
        protected void init() {
                // TODO Auto-generated method stub
                backgroundFloor.setWidth(800);
                backgroundFloor.setHeight(960);
                String strAch1 = engine.getDatabase().getData(ACHIEVEMENT_1, 0, 1);
        		String strAch2 = engine.getDatabase().getData(ACHIEVEMENT_2, 0, 1);
        		String strAch3 = engine.getDatabase().getData(ACHIEVEMENT_3, 0, 1);
        		String strAch4 = engine.getDatabase().getData(ACHIEVEMENT_4, 0, 1);
        		String strAch5 = engine.getDatabase().getData(ACHIEVEMENT_5, 0, 1);
        		String strAch6 = engine.getDatabase().getData(ACHIEVEMENT_6, 0, 1);
        		String strAch7 = engine.getDatabase().getData(ACHIEVEMENT_7, 0, 1);
        		String strAch8 = engine.getDatabase().getData(ACHIEVEMENT_8, 0, 1);
        		String strAch9 = engine.getDatabase().getData(ACHIEVEMENT_9, 0, 1);
        		String strAch10 = engine.getDatabase().getData(ACHIEVEMENT_10, 0, 1);
        		Ach1 = Integer.parseInt(strAch1);
        		Ach2 = Integer.parseInt(strAch2);
        		Ach3 = Integer.parseInt(strAch3);
        		Ach4 = Integer.parseInt(strAch4);
        		Ach5 = Integer.parseInt(strAch5);
        		Ach6 = Integer.parseInt(strAch6);
        		Ach7 = Integer.parseInt(strAch7);
        		Ach8 = Integer.parseInt(strAch8);
        		Ach9 = Integer.parseInt(strAch9);
        		Ach10 = Integer.parseInt(strAch10);
        		keluar = false;
        		if(Ach1 == 1){
        			acvpiclock[0].setVisible(false);
        		}
        		else{
        			acvpiclock[0].setVisible(true);
        		}
        		if(Ach2 == 1){
        			acvpiclock[1].setVisible(false);
        		}
        		else{
        			acvpiclock[1].setVisible(true);
        		}
        		if(Ach3 == 1){
        			acvpiclock[2].setVisible(false);
        		}
        		else{
        			acvpiclock[2].setVisible(true);
        		}
        		if(Ach4 == 1){
        			acvpiclock[3].setVisible(false);
        		}
        		else{
        			acvpiclock[3].setVisible(true);
        		}
        		if(Ach5 == 1){
        			acvpiclock[4].setVisible(false);
        		}
        		else{
        			acvpiclock[4].setVisible(true);
        		}
        		if(Ach6 == 1){
        			acvpiclock[5].setVisible(false);
        		}
        		else{
        			acvpiclock[5].setVisible(true);
        		}
        		if(Ach7 == 1){
        			acvpiclock[6].setVisible(false);
        		}
        		else{
        			acvpiclock[6].setVisible(true);
        		}
        		if(Ach8 == 1){
        			acvpiclock[7].setVisible(false);
        		}
        		else{
        			acvpiclock[7].setVisible(true);
        		}
        		if(Ach9 == 1){
        			acvpiclock[8].setVisible(false);
        		}
        		else{
        			acvpiclock[8].setVisible(true);
        		}
        		if(Ach10 == 1){
        			acvpiclock[9].setVisible(false);
        		}
        		else{
        			acvpiclock[9].setVisible(true);
        		}
        }

        @Override
        protected void attach() {
                // TODO Auto-generated method stub
                engine.scene.attachChild(backgroundFloor);
                engine.scene.attachChild(acvpic1);
                engine.scene.attachChild(acvpic2);
                engine.scene.attachChild(acvpic3);
                engine.scene.attachChild(acvpic4);
                engine.scene.attachChild(acvpic5);
                engine.scene.attachChild(acvpic6);
                engine.scene.attachChild(acvpic7);
                engine.scene.attachChild(acvpic8);
                engine.scene.attachChild(acvpic9);
                engine.scene.attachChild(acvpic10);
                for(int i=0; i<10; i++){
                        engine.scene.attachChild(acvpiclock[i]);
                }
                
                engine.scene.attachChild(achievement1);
                engine.scene.attachChild(achievement2);
                engine.scene.attachChild(achievement3);
                engine.scene.attachChild(achievement4);
                engine.scene.attachChild(achievement5);
                engine.scene.attachChild(achievement6);
                engine.scene.attachChild(achievement7);
                engine.scene.attachChild(achievement8);
                engine.scene.attachChild(achievement9);
                engine.scene.attachChild(achievement10);
                
                engine.scene.attachChild(achievementtxt1);
                engine.scene.attachChild(achievementtxt2);
                engine.scene.attachChild(achievementtxt3);
                engine.scene.attachChild(achievementtxt4);
                engine.scene.attachChild(achievementtxt5);
                engine.scene.attachChild(achievementtxt6);
                engine.scene.attachChild(achievementtxt7);
                engine.scene.attachChild(achievementtxt8);
                engine.scene.attachChild(achievementtxt9);
                engine.scene.attachChild(achievementtxt10);
        }

        @Override
        protected void detach() {
                // TODO Auto-generated method stub
                backgroundFloor.detachSelf();
                acvpic1.detachSelf();
                acvpic2.detachSelf();
                acvpic3.detachSelf();
                acvpic4.detachSelf();
                acvpic5.detachSelf();
                acvpic6.detachSelf();
                acvpic7.detachSelf();
                acvpic8.detachSelf();
                acvpic9.detachSelf();
                acvpic10.detachSelf();
                for(int i=0; i<10; i++){
                        acvpiclock[i].detachSelf();
                }
                
                achievement1.detachSelf();
                achievement2.detachSelf();
                achievement3.detachSelf();
                achievement4.detachSelf();
                achievement5.detachSelf();
                achievement6.detachSelf();
                achievement7.detachSelf();
                achievement8.detachSelf();
                achievement9.detachSelf();
                achievement10.detachSelf();
                
                achievementtxt1.detachSelf();
                achievementtxt2.detachSelf();
                achievementtxt3.detachSelf();
                achievementtxt4.detachSelf();
                achievementtxt5.detachSelf();
                achievementtxt6.detachSelf();
                achievementtxt7.detachSelf();
                achievementtxt8.detachSelf();
                achievementtxt9.detachSelf();
                achievementtxt10.detachSelf();
        }

        @Override
        protected void setPosition() {
                // TODO Auto-generated method stub
        		achievement1.setPosition(30, 30);
                acvpic1.setPosition(30, 60);
                acvpiclock[0].setPosition(30, 60);
                achievementtxt1.setPosition(160, 65);
                
                achievement2.setPosition(30, 180);
                acvpic2.setPosition(30, 210);
                acvpiclock[1].setPosition(30, 210);
                achievementtxt2.setPosition(160, 215);
                
                achievement3.setPosition(30, 330);
                acvpic3.setPosition(30, 360);
                acvpiclock[2].setPosition(30, 360);
                achievementtxt3.setPosition(160, 365);
                
                achievement4.setPosition(30, 480);
                acvpic4.setPosition(30, 510);
                acvpiclock[3].setPosition(30, 510);
                achievementtxt4.setPosition(160, 515);
                
                achievement5.setPosition(30, 630);
                acvpic5.setPosition(30, 660);
                acvpiclock[4].setPosition(30, 660);
                achievementtxt5.setPosition(160, 665);
                
                
                achievement6.setPosition(400, 30);
                acvpic6.setPosition(400, 60);
                acvpiclock[5].setPosition(400, 60);
                achievementtxt6.setPosition(530, 65);
                
                achievement7.setPosition(400, 180);
                acvpic7.setPosition(400, 210);
                acvpiclock[6].setPosition(400, 210);
                achievementtxt7.setPosition(530, 215);
                
                achievement8.setPosition(400, 330);
                acvpic8.setPosition(400, 360);
                acvpiclock[7].setPosition(400, 360);
                achievementtxt8.setPosition(530, 365);
                
                achievement9.setPosition(400, 480);
                acvpic9.setPosition(400, 510);
                acvpiclock[8].setPosition(400, 510);
                achievementtxt9.setPosition(530, 515);
                
                achievement10.setPosition(400, 630);
                acvpic10.setPosition(400, 660);
                acvpiclock[9].setPosition(400, 660);
                achievementtxt10.setPosition(530, 665);
                
                

        }

        @Override
        protected void registerTouch() {
                // TODO Auto-generated method stub
                engine.scene.registerTouchArea(backgroundFloor);
        }

        @Override
        protected void unregisterTouch() {
                // TODO Auto-generated method stub
                engine.unregisterSceneTouch(backgroundFloor);
        }

        @Override
        protected void onUpdate() {
                // TODO Auto-generated method stub
                if(keluar){
                	exitState(MainMenuState);
                }
        }

        @Override
        protected void onPaused() {
                // TODO Auto-generated method stub
                
        }

        @Override
        protected void onResumed() {
                // TODO Auto-generated method stub
                
        }

        @Override
        public void onKeyUp(int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
        	if(keyCode == KeyEvent.KEYCODE_BACK){
        		
    			keluar = true;
    			
    		}
        }

		@Override
		public void initLoading() {
			// TODO Auto-generated method stub
			
		}

}